package com.jkoss.wine.investment.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.util.ExcelUtiles;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.investment.entity.Investment;
import com.jkoss.wine.investment.service.IInvestmentService;
import com.jkoss.wine.investment.utils.MyUtils;
import com.jkoss.wine.investment.vo.InvestmentVo;
import com.jkoss.wine.system.entity.Areas;
import com.jkoss.wine.system.entity.MerchantEmployees;
import com.jkoss.wine.system.service.IAreasService;
import com.jkoss.wine.system.service.IMerchantEmployeesService;

/**
 * 
 * 
 * @Author ym
 * @Version 1.0, 2018年9月23日
 * @See
 * @Since com.jkoss.mengal.investment.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/investment/customer")
public class InvestmentController extends BaseController {

	@Autowired
	private IInvestmentService iInvestmentService;
	@Autowired
	private IAreasService iAreasService;
	@Autowired
	private IMerchantEmployeesService iMerchantEmployeesService;

	/**
	 * 列表
	 * 
	 * @param dwzPageBean
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/list")
	@RequiresPermissions("/investment/customer/list")
	public String list(String phone, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request,
			HttpServletResponse response) {
		System.out.println("进入list------------------------------------------");
		Wrapper wrapper = Condition.create().eq("i.del", 0);
		if (!CommonUtil.isBlank(phone)) {
			wrapper.eq("i.phone", phone);
		} 
		Page resultPage = iInvestmentService.selectVoByPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "investment/customer/list";
	}

	/**
	 * 去新增
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toInsert")
	@RequiresPermissions("/investment/customer/toInsert")
	public String toInsert(ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		// 获取所有省级
		Wrapper wrapper = Condition.create().andNew("id like '%0000'");
		List<Areas> list = iAreasService.selectList(wrapper);

		// 获取跟进人下拉列表
		Wrapper empWaper = Condition.create().eq("del", 0);
		List<MerchantEmployees> melist = iMerchantEmployeesService.selectList(empWaper);
		map.put("maids", melist);

		map.put("provinces", list);
		return "investment/customer/edit";
	}

	@RequestMapping("/insert")
	@RequiresPermissions("/investment/customer/toInsert")
	@ResponseBody
	public Object insert(@Valid Investment investment, ModelMap map, HttpServletRequest request,
			HttpServletResponse response) {
		// 添加时间
		investment.setAddTime(CommonUtil.getDate());
		// 操作人
		investment.setOperator(MyUtils.setOperator());
		// 是否删除 0否1是
		investment.setDel(0);
		if (iInvestmentService.insert(investment)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}

	/**
	 * 去修改
	 * 
	 * @param id
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toUpdate")
	@RequiresPermissions("/investment/customer/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		// 获取跟进人下拉列表
		Wrapper empWaper = Condition.create().eq("del", 0);
		List<MerchantEmployees> melist = iMerchantEmployeesService.selectList(empWaper);
		map.put("maids", melist);

		// 获取所有省级
		Wrapper wrapper = Condition.create().andNew("id like '%0000'");
		List<Areas> list = iAreasService.selectList(wrapper);
		map.put("provinces", list);

		map.put("record", iInvestmentService.selectById(id));
		String getareaName = iInvestmentService.selectById(id).getareaName();
		String areaCode = iInvestmentService.selectById(id).getareaCode();

		// 增强健壮，解析地名字符串
		if (!CommonUtil.isBlank(getareaName) && !CommonUtil.isBlank(areaCode)) {
			map.put("areaNames", getareaName.split(","));
			String city = areaCode.substring(0, 2);
			Wrapper wrapper1 = Condition.create().andNew("id like '" + city + "%00'").ne("id", city + "0000");
			List<Areas> list1 = iAreasService.selectList(wrapper1);
			map.put("citys", list1);

			String region = areaCode.substring(0, 4);
			Wrapper wrapper2 = Condition.create().andNew("id like '" + region + "%'").ne("id", region + "00");
			List<Areas> list2 = iAreasService.selectList(wrapper2);
			map.put("regions", list2);
		}

		return "investment/customer/edit";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/investment/customer/toUpdate")
	@ResponseBody
	public Object update(@Valid Investment investment, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("进入更新页面");
		System.out.println("inv:" + investment);
		if (iInvestmentService.updateById(investment)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 去删除
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("/investment/customer/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) {
		List list = new ArrayList();
		for (String string : id) {
			Investment investment = new Investment();
			investment.setId(string);
			// 是否删除：0-未删除、1-已删除、2-不能删除
			investment.setDel(1);
			list.add(investment);
		}
		if (iInvestmentService.updateBatchById(list)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}
	
	//导出excel
	@RequestMapping("/excel")
	@RequiresPermissions("/investment/customer/excel")
	public void excel(String phone,DwzPageBean dwzPageBean, ModelMap map,HttpServletResponse response) {
		System.out.println("进入excel----------"+phone);
		Wrapper wrapper = Condition.create();
		if (!CommonUtil.isBlank(phone)){										
			wrapper.eq("i.phone",phone);
		}
		ExcelUtiles.exportExcel(iInvestmentService.selectVoByList(wrapper), "招商信息", "招商记录", InvestmentVo.class, "招商信息.xls", response);
	}
	
	//导入excel
	@SuppressWarnings("unchecked")
	@RequestMapping("/importExcel")
	@RequiresPermissions("/investment/customer/importExcel")
	@ResponseBody
	public Object importExcel(MultipartFile file,DwzPageBean dwzPageBean, ModelMap map,HttpServletResponse response) {
		boolean insert = false;
		System.out.println("进入import----------"+file);
		List<InvestmentVo> importExcel = ExcelUtiles.importExcel(file, 1, 1, InvestmentVo.class);
		
		for (InvestmentVo investmentVo : importExcel) {
			Wrapper wrapper = Condition.create().eq("phone", investmentVo.getPhone());
			List phoneInv = iInvestmentService.selectList(wrapper);
			if(phoneInv==null&&investmentVo.getPhone()!=null) {
				investmentVo.setOperator(MyUtils.setOperator());
				investmentVo.setAddTime(CommonUtil.getDate());
				insert = iInvestmentService.insert(investmentVo);
			}
			System.out.println(investmentVo);
		}
		if (insert ) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}
	
	@RequestMapping("/toImportExcel")
	@RequiresPermissions("/investment/customer/toImportExcel")
	public Object toImportExcel(DwzPageBean dwzPageBean, ModelMap map,HttpServletResponse response) {
		return "investment/customer/importExcel";
	}
	
	
}
